
********************************************************************************
* Authors: Henrik Andersson & Sirus Dehdari
* Description: This script uses the precinct collapsed data for each election 
* year and collapses it further in terms of 2010/2014 combinations
* into 2014 precincts, using population weights for each combination.
* It finally merges the collapsed data together into one panel file with many 
* precinct information over three years, 2006, 2010 and 2014.  
********************************************************************************

clear all
cd "E:\"

* Setting path to temp folder:
local out_temp "E:\ProjData\IntegrationSD\temp\"



*** Henrik: här specificerar du namnet på den variabel som ligger allra sist i din variabellista.
* Variabeln "pop" ligger ju först, så vi sparar en local med namnet på den som ligger sist
local last_var = "sh_im_cw_noi_swe_rob"


*** Cleaning 2010 election data (changing precinct code and variable name):
use `out_temp'2010_electionresults, clear

* Adding "0" to districts where it has been dropped because of numeric:
tostring llkkdddd, replace
replace llkkdddd = "0" + llkkdddd if strlen(llkkdddd) < 8
rename llkkdddd precinct2010

* Merging election data with the precinct combos:
merge 1:m precinct2010 using `out_temp'\vd_2010_2014_shares_r
drop _merge

* Multiplying each election result (in numbers) with combo shares:
foreach var of varlist *tal RostGiltiga Rostande Rostb other pop-`last_var'{
quietly replace `var' = share*`var'
}

* Aggregate to 2014 precints:
collapse (sum) *tal RostGiltiga Rostande Rostb other pop-`last_var', by(precinct2014)

gen year = 2010

foreach var of varlist *tal{
local vname = substr("`var'", 1,strlen("`var'")-3)
gen `vname'proc = 100*`var'/(Mtal+Ctal+FPtal+KDtal+Stal+Vtal+MPtal+SDtal+FItal+PPtal+other)
}

* Create turnout variable:
gen VD = 100*Rostande/Rostb

* Dropping all the number of votes variables (DO WE WANT TO KEEP THEM?)
drop *tal

save `out_temp'reg_2010_2014, replace



*************************
*** 2006:
import excel D:\Data\ExtData\ValResData\Riksdagsval_2006\Riksdagsval_2006.xls, sheet("riksdagsvalet_vd_2006_orginal") firstrow clear

rename LKFV precinct2006

merge 1:m precinct2006 using `out_temp'vd_2006_2014_shares_r, keep(3)
drop _merge

foreach var of varlist *_ROST ROSTB pop-`last_var'{
quietly replace `var' = share*`var'
}

collapse (sum) *_ROST ROSTB pop- `last_var', by(precinct2014)

gen year = 2006

* Creating a "other parties" variable:
gen other_ROST = SPI_ROST + JL_ROST + SJVP_ROST + ÖVR_ROST

drop SPI_ROST JL_ROST SJVP_ROST ÖVR_ROST


foreach var of varlist *_ROST{
local vname = substr("`var'", 1,strlen("`var'")-5)
gen `vname'proc = 100*`var'/(M_ROST+C_ROST+FP_ROST+KD_ROST+S_ROST+V_ROST+MP_ROST+SD_ROST+FI_ROST+PP_ROST+other_ROST)
}

gen VD = 100*(M_ROST+C_ROST+FP_ROST+KD_ROST+S_ROST+V_ROST+MP_ROST+SD_ROST+FI_ROST+PP_ROST+other_ROST+BLANK_ROST)/ROSTB

* Dropping all the number of votes variables (DO WE WANT TO KEEP THEM?)
drop *_ROST



save `out_temp'reg_2006_2014, replace


///CREATE PANEL

///Now put the 2010, 2006 and 2014 file together and prepare panel

clear all
use "E:\ProjData\IntegrationSD\temp\2014.dta"
gen year=2014 
 append using "E:\ProjData\IntegrationSD\temp\reg_2010_2014.dta"
 replace year=2010 if year==.
 replace precinct2014=llkkddd if year==2014
 drop llkkdddd
  append using "E:\ProjData\IntegrationSD\temp\reg_2006_2014.dta"
  replace year=2006 if year==.

  *time variable
gen t=1 if year==2006
replace t=2 if year==2010
replace t=3 if year==2014

*panel setting
order precinct2014 year t
destring precinct2014, replace
xtset precinct2014 t
sort precinct2014 t

*duplicates tag precinct2014, gen(tag)
*bysort year: tab tag
*5,044 districts in 2010 and 2014 both years
*4712 districts all years
*272 enbart 2010
*709 enbart 2006
*659 enbart 2014
save "E:\ProjData\IntegrationSD\use\main.dta", replace



log close











